<?php

function get_user_bonus($user_id = 0)
{
	if($user_id == 0)
	{
		$user_id=$_SESSION['user_id'];
	}
	
	$sql = "SELECT SUM(bt.type_money) AS bonus_value,COUNT(*) as bonus_count ".
			"FROM ".$GLOBALS['ecs']->table('user_bonus')." AS ub, ".
			$GLOBALS['ecs']->table('bonus_type') . " AS bt ".
			"WHERE ub.user_id = '$user_id' AND ub.bonus_type_id = bt.type_id AND ub.order_id = 0";
			
	$row = $GLOBALS['db']->getRow($sql);
	return $row;
}

function get_dyna_libs($theme,$tmp)
{
	$ext = end(explode('.', $tmp));
	$tmp = basename($tmp,".$ext");
	$sql = 'SELECT region,library,sort_order,id,number,type' . 
			' FROM ' . $GLOBALS['ecs']->table('template') . 
			" WHERE theme = '$theme' AND filename = '" . $tmp ."' AND type > 0 AND remarks=''".
			' ORDER BY region,library,sort_order';
	$res = $GLOBALS['db']->getAll($sql);
	
	$dyna_libs = array();
	foreach($res as $row) 
	{
		$dyna_libs[$row['region']][$row['library']][] = array(
			'id' => $row['id'],
			'number'	=> $row['number'],
			'type'		=> $row['type'],
		);
	}
	
	return $dyna_libs;
}